package textui;

import static hacked.ReduceVerbosity.filter;
import static hacked.ReduceVerbosity.fold;
import hacked.ReduceVerbosity.Filter;
import hacked.ReduceVerbosity.Folder;

import java.util.ArrayList;
import java.util.List;

import textui.menu.MenuItem;

public class Menu extends UIElement {
	private List<MenuItem> _items;

	public Menu(List<MenuItem> item) {
		super(null);// statless menu
		_items = item;
	}

	@Override
	public UIElement run() {
		_items = filter(_items, new Filter<MenuItem>() {
			@Override
			public boolean accepts(MenuItem t) {
				return t.canBeCreated();
			}
		});
		return new ElementSelector<>(map(_items)).select().item.create();
	}

	private class f {
		private MenuItem item;

		f(MenuItem i) {
			this.item = i;
		}

		public String toString() {
			return item.name();
		}
	};

	private List<f> map(List<MenuItem> item) {
		return fold(new Folder<MenuItem, List<Menu.f>>(new ArrayList<f>()) {

			@Override
			public List<f> apply(MenuItem a, List<f> b) {
				b.add(new f(a));
				return b;
			}
		}, item);
	}
}